package com.i_art_engineer.meigong.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.i_art_engineer.meigong.AppContext;
import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.base.ActionBarActivity;
import com.i_art_engineer.meigong.util.CountUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 忘记密码界面
 */
public class ForgetActivity extends ActionBarActivity {
    private EditText et_mobile, et_yanzhengma ,et_newpassword,et_renewpassord;
    private Button btn_submit,btn_fetchcode;
    private boolean isget = false;

    private Handler timehandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what>0){
                btn_fetchcode.setText(msg.what+"秒后重新获取。");
                btn_fetchcode.setEnabled(false);
            }else{
                btn_fetchcode.setText("获取验证码");
                btn_fetchcode.setEnabled(true);
                isget = false;
            }

        }
    };

    private View.OnClickListener listener = new View.OnClickListener() {
        private boolean isget = false;
        @Override
        public void onClick(View v) {
            String mobile = et_mobile.getText().toString();
            String captcha = et_yanzhengma.getText().toString();
            String pwd1 = et_newpassword.getText().toString();
            String pwd2 = et_renewpassord.getText().toString();
            switch (v.getId()) {
                case R.id.btn_submit:

                    if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(captcha) || TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)) {

                        Toast.makeText(ForgetActivity.this, "必备信息不全，请重新填写！", Toast.LENGTH_SHORT).show();
                        break;

                    } else {

                        if (!pwd1.equals(pwd2)) {
                            Toast.makeText(ForgetActivity.this, "两次输入的密码不一致，请重新填写！", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        AsyncHttpResponseHandler handler = new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                Log.i("忘记密码提交", response.toString());
                                //{"code":200,"msg":"注册成功","data":{"ticket":"5YeTQ05ptQFR4lBFife9DpoQFWCskA3qDJ2FbnKENF4"}}

                                try {
                                    int code = response.getInt("code");
                                    String msg = response.getString("msg");
                                    if (code == 200) {

                                        Toast.makeText(ForgetActivity.this, "密码修改：" + msg, Toast.LENGTH_SHORT).show();
                                        setResult(2);
                                        finish();
                                    } else {
                                        //   {"code":201,"msg":"请输入正确的电话号码"}
                                        Toast.makeText(ForgetActivity.this, "密码修改：" + msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        };
                        AsyncHttpClient c = new AsyncHttpClient();
//                        AppHttpClient.setHttpClient(c);
                        String partUrl = "http://www.9fanr.com/app/account/forget";
                        RequestParams params = new RequestParams();
                        params.put("phone", mobile);
                        params.put("npwd", pwd1);
                        params.put("os", "android");
                        params.put("sms", captcha);
                        c.get(partUrl, params, handler);


                    }
                    break;
                case R.id.btn_fetchcaptcha:


                    if (TextUtils.isEmpty(mobile)) {
                        Toast.makeText(ForgetActivity.this, "必备信息不全，请重新填写！", Toast.LENGTH_SHORT).show();
                    } else {
                        AsyncHttpResponseHandler handler = new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                Log.i("忘记密码 获取验证码", response.toString());
                                //     {"code":200,"msg":"验证码已发送19"}
                                try {
                                    String msg = response.getString("msg");
                                    Toast.makeText(ForgetActivity.this, msg, Toast.LENGTH_SHORT).show();
                                    ;
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }
                        };
                        AsyncHttpClient c = new AsyncHttpClient();
//                        AppHttpClient.setHttpClient(c);
                        String partUrl = "http://www.9fanr.com/app/sms/send";
                        RequestParams params = new RequestParams();
                        params.put("phone", mobile);
                        c.post(partUrl, params, handler);
                        Toast.makeText(ForgetActivity.this, "忘记密码获取验证码", Toast.LENGTH_SHORT).show();
                        CountUtil timeCount= new CountUtil(30000,1000,btn_fetchcode, R.string.btn_fetchcaptcha);//传入了文字颜色值
                        timeCount.start();

                    //此处调用获取验证码接口
              }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        initActionBar(R.string.title_resetpwd);
        et_mobile = (EditText)findViewById(R.id.et_mobile);

        et_yanzhengma = (EditText)findViewById(R.id.et_yanzhengma);
        et_newpassword = (EditText)findViewById(R.id.et_newpassword);
        et_renewpassord = (EditText)findViewById(R.id.et_renewpassord);
        btn_submit = (Button)findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(listener);
        btn_fetchcode = (Button)findViewById(R.id.btn_fetchcaptcha);
        btn_fetchcode.setOnClickListener(listener);
    }

}
